package com.kehao.accountbook.adapter;

import android.app.Activity;
import android.content.Intent;
import android.icu.text.Transliterator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kehao.accountbook.AddExpendActivity;
import com.kehao.accountbook.ModifyExpendActivity;
import com.kehao.accountbook.R;
import com.kehao.accountbook.fragment.MainFragment;
import com.kehao.accountbook.pojo.Expenditure;

import java.util.List;


public class ExpenditureAdapter extends RecyclerView.Adapter<ExpenditureAdapter.ViewHolder> {
    public List<Expenditure> expenditureList;
    public ItemClickListener listener;

    public ExpenditureAdapter(List<Expenditure> expenditureList) {
        this.expenditureList = expenditureList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.expenditure_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Expenditure expenditure = expenditureList.get(position);
        switch (expenditure.getType()){
            case Expenditure.TYPE_EATTING:
                holder.imageView.setImageResource(R.drawable.expend_eatting);
                break;
            case Expenditure.TYPE_FINANCIAL:
                holder.imageView.setImageResource(R.drawable.expend_financial);
                break;
            case Expenditure.TYPE_NORMAL:
                holder.imageView.setImageResource(R.drawable.expend_normal);
                break;
            case Expenditure.TYPE_SHOPPING:
                holder.imageView.setImageResource(R.drawable.expend_shopping);
                break;
        }
        holder.tv_data.setText(expenditure.getDate().toString());
        holder.tv_time.setText(expenditure.getTime().toString());
        holder.tv_money.setText(expenditure.getMoney()<0?String.format("%.2f",expenditure.getMoney()):"+"+String.format("%.2f",expenditure.getMoney()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return expenditureList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tv_data;
        TextView tv_time;
        TextView tv_money;
        View itemView;
        public ViewHolder(@NonNull final View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            tv_data = itemView.findViewById(R.id.tv_date);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_money = itemView.findViewById(R.id.tv_money);
            this.itemView = itemView;
        }
    }
    public interface ItemClickListener{
        void onClick(View view, int position);
    }


}
